///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IMAGE_CPOLYGON_H_
#define _BUOLA_IMAGE_CPOLYGON_H_

#include <buola/image.h>
#include <buola/utility/cfixed.h>
#include <buola/utility/crect.h>
#include <buola/image/clinesegment.h>
#include <buola/image/ctrap.h>

namespace buola { namespace img {

class CPolygon
{
public:
    struct SEdge;

    struct SDeferredTrap
    {
        SEdge *mRight;
        CFixed<> mTop;
    };
    
    struct SEdge
    {
        CLineSegment mLine;
        int mDirection;

        SEdge *mNext; //also E1
        SEdge *mPrev; //also E2
        SDeferredTrap mDeferredTrap;
    };

    enum EEventType
    {
        BO_STOP,
        BO_INTERSECTION,
        BO_START
    };
    
    struct SEvent
    {
        EEventType mType;
        CPoint_x mPoint;

        SEdge mEdge;
    };

    class SSweepLine;
    class SEventQueue;
    
    void AddEdge(const CPoint_x &p1,const CPoint_x &p2,int pDirection)
    {
        if(p1.y==p2.y) return; //ignore horizontal lines
        
        SEvent lEvent;
        lEvent.mType=BO_START;

        if(p1.y<p2.y)
        {
            lEvent.mEdge.mLine={p1,p2};
            lEvent.mEdge.mDirection=pDirection;
        }
        else
        {
            lEvent.mEdge.mLine={p2,p1};
            lEvent.mEdge.mDirection=-pDirection;
        }

        lEvent.mEdge.mDeferredTrap.mRight=nullptr;

        lEvent.mPoint.y=lEvent.mEdge.mLine.mTop;
        lEvent.mPoint.x=lEvent.mEdge.mLine.GetXForY(lEvent.mPoint.y);
        mEdges.push_back(lEvent);
    }
    
    void AddContour(const std::vector<CPoint_x> &pContour,int pDirection)
    {
        if(pContour.size()<=1) return;

        for(int i=0;i<pContour.size()-1;i++)
        {
            AddEdge(pContour[i],pContour[i+1],pDirection);
        }
    }

    void Print()
    {
        msg_info() << "begin polygon\n";
        for(int i=0;i<mEdges.size();i++)
        {
            msg_info() << CPoint_d(mEdges[i].mEdge.mLine.m1) << CPoint_d(mEdges[i].mEdge.mLine.m2) << "\n";
        }
        msg_info() << "end polygon\n";
    }

    std::vector<CTrap> TesselateToTraps();
    
private:
//    CRect_x mExtents;
//    CRect_x mLimit;

    std::vector<SEvent> mEdges;
};

/*namespace img*/ } /*namespace buola*/ }

#endif
